/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow
 */

import React, {Component} from 'react';

// 使用自带插件 Navigator
import {Platform, StyleSheet, Text, View,Image} from 'react-native';
import {Navigator} from 'react-native-deprecated-custom-components';

// 使用 TabNavigator 外部插件 react-native-tab-navigator
import TabNavigator from 'react-native-tab-navigator';

// 引入项目自定义外部组件
import GDHome from '../home/GDHome';
import GDHourList from '../hourList/GDHourList';
import GDHt from '../ht/GDHt'

const instructions = Platform.select({
    ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
    android:
    'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

// type Props = {};
export default class GDMain extends Component{

    constructor(props) {
        super(props);
        this.state = {
            selectedTab: "home",
        };
    }

    //初始化tabBar

    // 注意：renderIcon是传入一个函数，这个函数返回一个Image组件，Image通过url加载，一定要记得设置尺寸，否则不显示
    // 安卓加载图片
    // 需要生成一个文件夹，名称固定drawable-xxhdpi，把图片放入这个文件夹中，然后放入安卓文件的res文件夹中
    renderTabNavigator(title,selectedTab,iconUrl,selectedIconUrl,component){
        return (
            <TabNavigator.Item
                selected={this.state.selectedTab === selectedTab}
                title={title}
                renderIcon={() => <Image source={{uri:iconUrl}} style={styles.tabbarIconStyle}/>}
                renderSelectedIcon={() => <Image source={{uri:selectedIconUrl}} style={styles.tabbarIconStyle}/>}
                //角标 badgeText="1"
                selectedTitleStyle={{color:'black'}}
                onPress={() => this.setState({ selectedTab: selectedTab })}>


                <Navigator
                    initialRoute={{
                        name:selectedTab,
                        component:component
                    }}
                    renderScene={(route, navigator) => {
                        let Component = route.component;
                        return <Component {...route.params} navigator={navigator}/>
                    }}/>
            </TabNavigator.Item>
        );
    }



    render() {
        return (
            <TabNavigator>
                {this.renderTabNavigator("主页","home","tabbar_home_30x30","tabbar_home_selected_30x30",GDHome)}
                {this.renderTabNavigator("海淘","ht","tabbar_abroad_30x30","tabbar_abroad_selected_30x30",GDHourList)}
                {this.renderTabNavigator("一小时","yxs","tabbar_rank_30x30","tabbar_rank_selected_30x30",GDHt)}
            </TabNavigator>

        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },

    tabbarIconStyle:{
        width:Platform.OS==="ios"?30:25,
        height:Platform.OS==="ios"?30:25
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
});
